//
//  FSwaitdetailViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2018/4/13.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "GDataDefines.h"
#import "yuebanxxController.h"

#import "OANameListController.h"
#import "OASetMeetingOpinionController.h"
#import "OAExpendViewController.h"
#import "shouwenViewController.h"
#import "FSWaitDetailController.h"
#import <QuickLook/QuickLook.h>
#import "FSwaitdetailViewController.h"
#import "bfxxTableViewController.h"
@interface FSwaitdetailViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>

@property(nonatomic,strong)NSMutableArray * contentsData;
@property(nonatomic,strong)NSMutableArray * attachmentArray;
@property(nonatomic,strong)NSMutableArray * mainattachmentArray; //正文附件
@property (strong, nonatomic) IBOutlet UIView *daiyueview;
- (IBAction)yiyueaction:(id)sender;
- (IBAction)chuanyueaction:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *huiyishangbao;
@property (strong, nonatomic) IBOutlet UIView *chuanyue;

- (IBAction)chuanyue:(id)sender;

@property (strong, nonatomic) IBOutlet UIView *footView;//已收文件

@property (strong, nonatomic) IBOutlet UIView *readRecordFootView;//只有阅读记录

@property (strong, nonatomic) IBOutlet UIView *psjFootView;//批事件


@property (strong, nonatomic) IBOutlet UIView *meetingFootView;


@property (weak, nonatomic) IBOutlet UIButton *btnTitle;

@property (weak, nonatomic) IBOutlet UIButton *opinionButton;

//填写意见
- (IBAction)operationAction:(UIButton *)sender;

//阅读记录
- (IBAction)readingRecordAction:(UIButton *)sender;

//参会人员
- (IBAction)joinMeetingPeopleList:(UIButton *)sender;

//批示件回复

- (IBAction)psjRecordAction:(UIButton *)sender;



@property(nonatomic,strong)NSString * attacmentURL;
@property(nonatomic,copy)NSString * replay; //是否需要回复
@end

@implementation FSwaitdetailViewController

-(NSMutableArray*)contentsData{
    
    if (_contentsData == nil) {
        _contentsData = [NSMutableArray array];
    }
    return _contentsData;
}
-(NSMutableArray*)attachmentArray{
    
    if (_attachmentArray == nil) {
        _attachmentArray = [NSMutableArray array];
    }
    return _attachmentArray;
}
-(NSMutableArray*)mainattachmentArray{
    if (_mainattachmentArray == nil) {
        _mainattachmentArray = [NSMutableArray array];
    }
    return _mainattachmentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.bounces = NO;
            self.edgesForExtendedLayout = UIRectEdgeNone;

//            self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.title = @"详情";
    
    [self requestData];
    if (_vcType==AlreadySignType){
    
     UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"启用流程" style:UIBarButtonItemStylePlain target:self action:@selector(qiyongAction)];
        self.navigationItem.rightBarButtonItem = rightIem;
    }
}
-  (void)qiyongAction{

{
 UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

         OADetailDisPatchController * detailVC = (OADetailDisPatchController*)[storyBoard instantiateViewControllerWithIdentifier:@"OADetailDispatchVC"];
//    detailVC.fileId = _detailPatchModel.fileId;
    detailVC.fileId = self.identifier;

    detailVC.isAlreadDoneWork = YES;
                detailVC.isUnDoWork = NO;
detailVC.type = @"dengji";
    [self.navigationController pushViewController:detailVC animated:YES];

}
}
- (void)viewWillAppear:(BOOL)animated{
    self.view.window.backgroundColor = [UIColor whiteColor];


}
- (void) viewDidAppear:(BOOL)animated{
//    self.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight-LL_TabbarSafeBottomMargin-49-kTopHeight);
//        self.tableView.X

////bomview
//_bomview = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-LL_TabbarSafeBottomMargin-49-kTopHeight, KScreenWidth, 49)];
//_bomview.backgroundColor = [UIColor whiteColor];
//[self.view addSubview:_bomview];
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//
//    CGFloat sectionHeaderHeight = 49;//设置你footer高度
//    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
//        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//    }
//
//}
-(void)requestData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.identifier},@{@"userId":userId}];
    __weak typeof(self) weakSelf = self;
//    WaitEndReadType
    NSString * url = nil;
    NSString * method = nil;
    if (self.vcType == WaitForReadType) {//待阅
        url = KexchgeneralURL;
        method = kContentReadFile;
           UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"阅办信息"style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];

                     self.navigationItem.rightBarButtonItem = item1;
        
    }else if (self.vcType == WaitEndReadType)//已阅
    {
        url = KexchgeneralURL;
        method = kContentReadFile;
    
    }
    else if (self.vcType == SenderSinType)//已发
    {
            if ([self.exchangeForm isEqualToString:@"form_meeting"] ) {
              url = KexchangeSubstanceURL;
        method = @"attendExpoSendMeeting";
            
            }else if ([self.exchangeForm isEqualToString:@"simplefile"]||[self.exchangeForm isEqualToString:@"form_mail"] ) {
              url = KexchangeSubstanceURL;
        method = @"contentSendExhcange";
            
            }
    
    
    }else if (self.vcType == draftExhcangePage)//草稿
    {
         if ([self.exchangeForm isEqualToString:@"form_meeting"] ) {
              url = KexchangeDraftURL;
        method = @"draftMeeting";
            
            }else if ([self.exchangeForm isEqualToString:@"simplefile"]||[self.exchangeForm isEqualToString:@"form_mail"] ) {
              url = KexchangeDraftURL;
        method = @"draftExhcange";
            
            }
    
    
    }else if(self.vcType ==    AlreadySignType)//已收
    {
         if ([self.exchangeForm isEqualToString:@"form_meeting"] ) {
              url = KexchangeSubstanceURL;
        method = @"attendExpoMeeting";
            
            }else if ([self.exchangeForm isEqualToString:@"simplefile"]||[self.exchangeForm isEqualToString:@"form_mail"] ) {
              url = KexchangeSubstanceURL;
        method = @"contentExhcange";
            
            }
    
    
    }
    else{
        url = KexchangeSubstanceURL;
        method = KcontentExhcangeMethod;
    }
    
    [HttpTools requestWithURL:url arrays:array method:method success:^(id JSON) {
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    NSLog(@"%@",JSON);
//    NSDictionary *dicc = [[NSDictionary alloc]init];
        NSArray * dataArrary = JSON[@"data"];
      NSMutableArray * values = [NSMutableArray array];

        if (dataArrary.count) {
       


// if (self.vcType == WaitForReadType||self.vcType == WaitEndReadType) {//待阅
//     values = dataArrary;
//
//    }else{
     NSDictionary *dic =[dataArrary firstObject];
            NSMutableDictionary *dict = [dic mutableCopy];

            for (NSString  * key in dict.allKeys)
            {
//              NSLog(@"allValues: %@", values);
              NSString *value = dict[key];
            
            if ([value isEqualToString:@""]) {
            
            [dict removeObjectForKey:key];
            
            }
                            [values addObject:value];

           }
            }
            
//            for (NSString  * value in dict.allValues) {
//            }
            weakSelf.contentsData = values;
            
            
//        }
        self.replay = JSON[@"replay"]; //是否需要回复
        
        if (self.vcType == SenderSinType) {
            if ([self.exchangeForm isEqualToString:@"form_meeting"] ) { //有会议参员  阅读记录
                self.meetingFootView.frame = CGRectMake(0, 0, KScreenWidth, 49);
//                self.tableView.tableFooterView = self.meetingFootView;
                 [self.bomview addSubview: self.meetingFootView];

                
            }else if([self.exchangeForm isEqualToString:@"form_psj"]){
                self.psjFootView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.tableView.tableFooterView = self.psjFootView;
                                [self.bomview addSubview: self.psjFootView];

                
            }else if([self.exchangeForm isEqualToString:@"simplefile"] || [self.exchangeForm isEqualToString:@"form_mail"]){
                
                self.readRecordFootView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
                                [self.bomview addSubview: self.readRecordFootView];

//                self.tableView.tableFooterView = self.readRecordFootView;
                
            }else {
                self.tableView.tableFooterView = [UIView new];
            }
            
        }else if (self.vcType ==draftExhcangePage)//草稿
        {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, KScreenWidth, 49);
      [button setTitle:@"发 送" forState:UIControlStateNormal];
      button.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(fassong) forControlEvents:UIControlEventTouchUpInside];
        [self.bomview addSubview: button];

        
        }
        else if (self.vcType == AlreadySignType){
                         UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"收文登记" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
                        //暂时去掉
//        self.navigationItem.rightBarButtonItem = rightIem;

            if ([self.exchangeForm isEqualToString:@"form_meeting"] ) {
             if ([self.replay isEqualToString:@"YES"] ) {
            [self.huiyishangbao setTitle:@"会议上报名单" forState:UIControlStateNormal];//需要回复
                self.footView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.footView];
                }else{
                 self.zhuanfaview.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.zhuanfaview];
                
                }

        }
        else if([self.exchangeForm isEqualToString:@"form_psj"]){
        if ([self.replay isEqualToString:@"YES"] ){
                self.psjFootView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.tableView.tableFooterView = self.psjFootView;
                                [self.bomview addSubview: self.psjFootView];
}else{
                 self.zhuanfaview.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.zhuanfaview];
    
                }
            
            }else if([self.exchangeForm isEqualToString:@"simplefile"] || [self.exchangeForm isEqualToString:@"form_mail"]){
            if ([self.replay isEqualToString:@"YES"] ){ //需要回复
          
                self.footView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.footView];
                }else{
                 self.zhuanfaview.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.zhuanfaview];
                
                }
                
            }else {
                self.tableView.tableFooterView = [UIView new];
            }
            
        }
        
        else if (self.vcType == WaitForReadType){


                self.daiyueview.frame = CGRectMake(0, 0, KScreenWidth, 49);

                [self.bomview addSubview: self.daiyueview];

            
        }else if (self.vcType == WaitForSignType){


                self.qianshou.frame = CGRectMake(0, 0, KScreenWidth, 49);


                [self.bomview addSubview: self.qianshou];

            
        }else if (self.vcType == WaitEndReadType){
          
//            self.tableView.tableFooterView = nil;
                        [self.btnTitle setTitle:@"传 阅" forState:UIControlStateNormal];
            [self.btnTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.btnTitle.titleLabel.font = [UIFont systemFontOfSize:18];
            self.chuanyue.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//            self.tableView.tableFooterView = self.readRecordFootView;
                [self.bomview addSubview: self.chuanyue];

        }else{
            if ([self.replay isEqualToString:@"YES"] ) { //需要回复
                self.footView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);
//                self.footView.backgroundColor = [UIColor yellowColor];
//                self.tableView.tableFooterView = self.footView;
                [self.bomview addSubview: self.footView];

                 UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"收文登记" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
//        self.navigationItem.rightBarButtonItem = rightIem;
        
            }else{
                self.tableView.tableFooterView = [UIView new];
//             self.footView.backgroundColor = [UIColor yellowColor];
 UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"收文登记" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
//        self.navigationItem.rightBarButtonItem = rightIem;

               self.footView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 49);

            }
            
        }
        
        self.attachmentArray = JSON[@"fj"][@"attachmet"];
//        self.mainattachmentArray = JSON[@"zw"][@"attachmet"];

        [weakSelf.tableView reloadData];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        
    }];
    
}
- (void)searchAction{
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"AnJiMain" bundle:nil];

 shouwenViewController * recordViewVC = (shouwenViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"shouwen"];

recordViewVC.fileId = _identifier;
    [self.navigationController pushViewController:recordViewVC animated:YES];




}
- (void)rightButtonAction{

  yuebanxxController * recordViewVC = (yuebanxxController*)[self.storyboard instantiateViewControllerWithIdentifier:@"ybxx"];
       recordViewVC.identifier =  self.identifier;
//       recordViewVC.type =  @"bfxx";

        recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
//    return 3;
        return 2;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return self.contentsData.count;
    }
//    else if (section == 1){
//        return self.mainattachmentArray.count;
//    }
    else{
        return self.attachmentArray.count;
    }
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSString * FileDetailCellId = @"FileDetailCellId";
        FSWaitDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:FileDetailCellId];
        if (cell == nil) {
//            cell = [[[NSBundle mainBundle] loadNibNamed:@"FSWaitDetailCell" owner:self options:nil] lastObject];
                        cell = [[[NSBundle mainBundle] loadNibNamed:@"FileDetailCell" owner:self options:nil] lastObject];

        }
        NSString * textString = self.contentsData[indexPath.row];
        NSArray * textArray = [textString componentsSeparatedByString:@"##"];
        if (textArray.count) {
            cell.titleLabel.text =[NSString stringWithFormat:@"%@:",[textArray firstObject]] ;
          cell.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            if([[textArray lastObject] isEqualToString:@"null"]){
            
                        cell.subjectLabel.text = @"";

            }else{
            
            
            cell.subjectLabel.text = [textArray lastObject];
            }
   

        }
        return cell;
    }
//    else if (indexPath.section == 1){
//
//
//        NSString * cellId = @"cellId";
//        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//        if (cell == nil) {
//            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
//            cell.textLabel.font = [UIFont systemFontOfSize:14];
//            cell.textLabel.textColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0];
//            cell.textLabel.numberOfLines = 0;
//        }
//        NSDictionary * dict = self.mainattachmentArray[indexPath.row];
//        cell.textLabel.text = dict[@"attachmentName"];
//        return cell;
//
//    }
    else {
        NSString * cellId = @"cellId";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.textLabel.textColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0];
            cell.textLabel.numberOfLines = 0;
        }
        NSDictionary * dict = self.attachmentArray[indexPath.row];
        cell.textLabel.text = dict[@"attachmentName"];
        return cell;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (section==1) {
//        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
//        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth, 21)];
//        label.text = @"正文";
//       // label.backgroundColor = [UIColor grayColor];
////        label.font = [UIFont systemFontOfSize:14];
////        label.textColor = [UIColor colorWithRed:85/255.0 green:85/255.0  blue:85/255.0 alpha:1.0];
// UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, .5)];
//        lineview.backgroundColor = [UIColor colorWithHexString:@"a1a1a1"];
//                [bgView addSubview:lineview];
//
//      label.font = [UIFont systemFontOfSize:16];
//        label.textColor = [UIColor blackColor];
//        [bgView addSubview:label];
////OAAttachmentsModel * model = self.mainattachmentArray[0];
////   if (_mainattachmentArray.count>0) {
////            OAAttachmentsModel * model = self.mainattachmentArray[0];
////            if ([model.attachmentsTitle length]>0) {
////        return bgView;
////
////}
////else{
////        return nil;
////
////}
////
////
////}else{
////        if (!(self.mainattachmentArray.count>0)) {
////            label.hidden = YES;
////        }else{
////            label.hidden = NO;
////        }
////         if (!(self.mainattachmentArray.count>0)) {
////        return nil;
////        }else{
////        return bgView;
////        }
////        }
//        return bgView;
//    }
     if (section==1) {
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
      UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
        lineview.backgroundColor = [UIColor colorWithHexString:@"C8C7CC"];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth, 21)];
        label.text = @"附件";
 if (_mainattachmentArray.count>0) {

}else{
            [bgView addSubview:lineview];

}
//        label.backgroundColor = [UIColor grayColor];
//        label.font = [UIFont systemFontOfSize:14];
//        label.textColor = [UIColor colorWithRed:85/255.0 green:85/255.0  blue:85/255.0 alpha:1.0];
      label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor blackColor];
        [bgView addSubview:label];
//         if (_attachmentArray.count>0) {
//            OAAttachmentsModel * model = self.attachmentArray[0];
//            if ([model.attachmentsTitle length]>0) {
//        return bgView;
//
//}
//else{
//        return nil;
//
//}
//}else{
//     if (!(self.attachmentArray.count>0)) {
//            label.hidden = YES;
//        }else{
//            label.hidden = NO;
//        }
//         if (!(self.attachmentArray.count>0)) {
//        return nil;
//        }else{
//        return bgView;
//        }
//        }
//}
        return bgView;

}
    return nil;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
//    if (_mainattachmentArray.count>0) {
//            OAAttachmentsModel * model = self.mainattachmentArray[0];
//            if ([model.attachmentsTitle length]>0) {
//         return 21;
//
//}
//else{
// return 0.001;
//[_mainattachmentArray removeAllObjects];
//
//}
//}else{
//    if (_mainattachmentArray.count !=0) {
//     return 21;
//
//}else{
//
// return 0.001;
//
//
//}
//    }
    return 30;
           }
    if (section==2) {
//        if (_attachmentArray.count>0) {
//            OAAttachmentsModel * model = self.attachmentArray[0];
//            if ([model.attachmentsTitle length]>0) {
//         return 21;
//
//}
//else{
//[_attachmentArray removeAllObjects];
// return 0.001;
//
//}
//}else{
// if (_attachmentArray.count!=0) {
//     return 21;
//
//}else{
//
// return 0.001;
//
//
//}}
return 30;
    }
    return 0.001;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (indexPath.section == 1) { //下载附件 查看附件信息
//
//        [tableView deselectRowAtIndexPath:indexPath animated:YES];
//        NSDictionary * dict = self.mainattachmentArray[indexPath.row];
//        NSString * url =  dict[@"attachmentContent"];
////        fujianwebViewController *vc = [[fujianwebViewController alloc]init];
////        vc.url = url;
////        [self.navigationController pushViewController:vc animated:YES];
//        [self openFileURL:url];
////  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
//
//    }else
     if (indexPath.section == 1){
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary * dict = self.attachmentArray[indexPath.row];
        NSString * url =  dict[@"attachmentContent"];
        [self openFileURL:url];
//  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
//  fujianwebViewController *vc = [[fujianwebViewController alloc]init];
//        vc.url = url;
//        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat height = 44;
    if (indexPath.section == 0) {
        NSString * textString = self.contentsData[indexPath.row];
        NSArray * textArray = [textString componentsSeparatedByString:@"##"];
        if (textArray.count) {
            NSString * titleString =[NSString stringWithFormat:@"%@:",[textArray firstObject]] ;
            NSString * subjectString = [textArray lastObject];
            CGFloat titleWidth = [NSString sizebyText:titleString font:14 size:CGSizeMake(MAXFLOAT, 0)].width;
            
            CGFloat subjectHeigth = [NSString sizebyText:subjectString font:14 size:CGSizeMake(KScreenWidth - 13 - titleWidth, MAXFLOAT)].height;
            height = subjectHeigth+10;
            if (height < 44) {
                height = 44;
            }
            return height;
        }
        
    }
//    else if(indexPath.section == 1){
//        NSDictionary * dict = self.mainattachmentArray[indexPath.row];
//        height = [NSString sizebyText:dict[@"attachmentName"] font:14 size:CGSizeMake(KScreenWidth - 30, MAXFLOAT)].height + 10;
//        if (height < 44) {
//            height = 44;
//        }
//        return height;
//    }
    else {
        NSDictionary * dict = self.attachmentArray[indexPath.row];
        height = [NSString sizebyText:dict[@"attachmentName"] font:14 size:CGSizeMake(KScreenWidth - 30, MAXFLOAT)].height + 10;
        if (height < 44) {
            height = 44;
        }
        return height;
    }
    return height;
}

-(void)openFileURL:(NSString*)url{
    NSString * stringurl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.attacmentURL =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]]; //附件路径
    if ([self isExtistFilePathWithFileName:[[NSURL URLWithString:stringurl] lastPathComponent]]) { //已经存在附件
        QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
        previewoCntroller.delegate=self;
        previewoCntroller.dataSource=self;
        previewoCntroller.navigationItem.rightBarButtonItem=nil;
        [self.navigationController pushViewController: previewoCntroller animated:YES];
        
    }else{
        MBProgressHUD * hud  =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"下载中...";
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringurl]];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            [data writeToFile:[filePath stringByAppendingPathComponent:[response.URL lastPathComponent]] atomically:YES];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
            previewoCntroller.delegate=self;
            previewoCntroller.dataSource=self;
            [self.navigationController pushViewController: previewoCntroller animated:YES];
            
        }];
    }
    
}

#pragma mark PreviewDataSource
//第一个方法是设定一组文档浏览的个数，会影响到在文档预览中的上/下一页。
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
//第二个方法是指定文档预览的文件路径
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    
    return [NSURL fileURLWithPath:self.attacmentURL];
}

-(BOOL)isExtistFilePathWithFileName:(NSString*)fileName{
    NSString * file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString * filePath = [file stringByAppendingPathComponent:fileName];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath] && fileName.length != 0) {
        return YES;
    }else{
        return NO;
    }
    
}



#pragma mark 填写意见
- (IBAction)operationAction:(UIButton *)sender {
    
    if ([self.exchangeForm isEqualToString:@"simplefile"]||[self.exchangeForm isEqualToString:@"form_mail"]) {
        [UIAlertView showWithTitle:nil message:@"请填写意见" style:UIAlertViewStylePlainTextInput cancelButtonTitle:nil otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            UITextField * textField = [alertView textFieldAtIndex:0];
            [textField resignFirstResponder];
            NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
            NSArray * array =@[@{@"userId":userId},@{@"unid":self.identifier},@{@"formxml":textField.text}];
            if ([textField.text length] == 0) {
                return;
            }
            [HttpTools requestWithURL:KexchexchangeURL arrays:array method:KSetSubmitBumphMethod success:^(id JSON) {
                [self.view.window makeToast:@"提交意见成功"];
            } failure:^(NSError *error) {
                [self.view.window makeToast:@"提交意见失败"];
            }];
        }];
        
    }else if([self.exchangeForm isEqualToString:@"form_meeting"]){
    //3
          UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"AnJiMain" bundle:nil];
        OASetMeetingOpinionController * setMeetingOpinion = (OASetMeetingOpinionController*)[storyBoard instantiateViewControllerWithIdentifier:@"MeetingOpinionVC"];
        setMeetingOpinion.identifier = self.identifier;

    

    
//        FSMeetingOpinionController * setMeetingOpinion = (FSMeetingOpinionController*)[self.storyboard instantiateViewControllerWithIdentifier:@"MeetingOpinion"];
//        setMeetingOpinion.identifier = self.identifier;
//        setMeetingOpinion.hidesBottomBarWhenPushed = YES;

        [self.navigationController pushViewController:setMeetingOpinion animated:YES];
        
    }else if ([self.exchangeForm isEqualToString:@"form_psj"]){
        
        FSPSJOpinionViewController * psjVC = (FSPSJOpinionViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"PSJOpinionVC"];
        psjVC.identifier = self.identifier;
                psjVC.hidesBottomBarWhenPushed = YES;

        [self.navigationController pushViewController:psjVC animated:YES];
    }

    
}
- (void)popview{

[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 阅读记录 // 或者 待阅详情已阅
- (IBAction)readingRecordAction:(UIButton *)sender {
    if (self.vcType == WaitForReadType) { //已阅
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray * array =@[@{@"unid":self.identifier},@{@"userId":userId}];
        [HttpTools requestWithURL:KexchgeneralURL arrays:array method:@"setSignRead" success:^(id JSON) {
if ([[NSString stringWithFormat:@"%@",JSON]isEqualToString:@"20"]) {
       [self.view.window makeToast:@"已阅成功"];
   self.btnTitle.enabled = NO;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"KrequestData" object:nil];
                        [self performSelector: @selector(popview) withObject: nil afterDelay:1.0f];

}else{

            [self.view.window makeToast:@"已阅失败"];

}

        } failure:^(NSError *error) {
            [self.view.window makeToast:@"已阅失败"];
        }];
        
        
    }
else if (self.vcType == WaitEndReadType){
 UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    OANameListController * expendVC = (OANameListController*)[storyBoard instantiateViewControllerWithIdentifier:@"NameLIstID"];
//OAExpendViewController * expendVC = [[OAExpendViewController alloc] init];
//    expendVC.identifierString = listModel.path;
                         expendVC.hidesBottomBarWhenPushed = YES;
//                                 expendVC.type = UnitDataType;
expendVC.seltype = @"chuanyue";

expendVC.fileId = self.identifier;
    [self.navigationController pushViewController:expendVC animated:YES];

}
    else{
        FSReadRecordController * recordViewVC = (FSReadRecordController*)[self.storyboard instantiateViewControllerWithIdentifier:@"ReadRecordVC"];
        recordViewVC.identifier =  self.identifier;
        recordViewVC.exchangeForm = self.exchangeForm;
        recordViewVC.hidesBottomBarWhenPushed = YES;

        [self.navigationController pushViewController:recordViewVC animated:YES];
    }
    
    
    
}

//参会人员
- (IBAction)joinMeetingPeopleList:(UIButton *)sender {
    
    FSReadRecordController * recordViewVC = (FSReadRecordController*)[self.storyboard instantiateViewControllerWithIdentifier:@"ReadRecordVC"];
    recordViewVC.identifier =  self.identifier;
    recordViewVC.exchangeForm = self.exchangeForm;
    recordViewVC.isMeetingList = YES;
    recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];

    
}

//批事件回复
- (IBAction)psjRecordAction:(UIButton *)sender {
    
    FSReadRecordController * recordViewVC = (FSReadRecordController*)[self.storyboard instantiateViewControllerWithIdentifier:@"ReadRecordVC"];
    recordViewVC.identifier =  self.identifier;
    recordViewVC.exchangeForm = self.exchangeForm;
    recordViewVC.isFromPSJType = YES;
        recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];
    
}


- (IBAction)yiyueaction:(id)sender {

   NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray * array =@[@{@"unid":self.identifier},@{@"userId":userId}];
        [HttpTools requestWithURL:KexchgeneralURL arrays:array method:@"setSignRead" success:^(id JSON) {
if ([[NSString stringWithFormat:@"%@",JSON]isEqualToString:@"20"]) {
       [self.view.window makeToast:@"已阅成功"];
   self.btnTitle.enabled = NO;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"KrequestData" object:nil];
                        [self performSelector: @selector(popview) withObject: nil afterDelay:1.0f];

}else{

            [self.view.window makeToast:@"已阅失败"];

}

        } failure:^(NSError *error) {
            [self.view.window makeToast:@"已阅失败"];
        }];
}
- (void)fassong{
  NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
                NSArray * array = @[@{@"userId":userId},@{@"unid":_identifier},@{@"json":_identifier}];
                                __weak typeof(self) weakSelf = self;

[HttpTools requestWithURL:KexchangeDraftURL arrays:array method:@"sendExhcange" success:^(id JSON) {
                    [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                      if ([[NSString stringWithFormat:@"%@",JSON] isEqualToString:@"20"]) {
                   [weakSelf.view.window makeToast:@"发送成功"];

                      
                      }else{
                                          [weakSelf.view.window makeToast:@"发送失败"];

                      }
//                   self.vcType = AlreadySignType;
                    [self requestData]; //刷新数据
    
    
                } failure:^(NSError *error) {
                    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                    [weakSelf.view.window makeToast:@"发送失败"];
                }];

}
- (IBAction)chuanyueaction:(id)sender {
          UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// if (_textString.length>0) {
//}else{
//    _textString = @"";
//
//}
    OANameListController * expendVC = (OANameListController*)[storyBoard instantiateViewControllerWithIdentifier:@"NameLIstID"];

//OAExpendViewController * expendVC = [[OAExpendViewController alloc] init];
//    expendVC.identifierString = listModel.path;
                         expendVC.hidesBottomBarWhenPushed = YES;
//                                 expendVC.type = UnitDataType;
expendVC.seltype = @"chuanyue";
//expendVC.textString = _textString;
expendVC.fileId = self.identifier;
    [self.navigationController pushViewController:expendVC animated:YES];

}
- (IBAction)zhuanfa:(id)sender {
 UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    OANameListController * expendVC = (OANameListController*)[storyBoard instantiateViewControllerWithIdentifier:@"NameLIstID"];
//OAExpendViewController * expendVC = [[OAExpendViewController alloc] init];
//    expendVC.identifierString = listModel.path;
                         expendVC.hidesBottomBarWhenPushed = YES;
//                                 expendVC.type = UnitDataType;
expendVC.seltype = @"zhuanfaxx";

expendVC.fileId = self.identifier;
    [self.navigationController pushViewController:expendVC animated:YES];
}

- (IBAction)qita:(id)sender {
    
    bfxxTableViewController * recordViewVC = (bfxxTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"bfxx"];
       recordViewVC.identifier =  self.identifier;
       recordViewVC.type =  @"qtxx";

        recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];
    
}

- (IBAction)zfxx:(id)sender {
    
    bfxxTableViewController * recordViewVC = (bfxxTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"bfxx"];
       recordViewVC.identifier =  self.identifier;
       recordViewVC.type =  @"zfxx";

        recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];
    
}

- (IBAction)bfxx:(id)sender {
    
    bfxxTableViewController * recordViewVC = (bfxxTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"bfxx"];
       recordViewVC.identifier =  self.identifier;
       recordViewVC.type =  @"bfxx";

        recordViewVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:recordViewVC animated:YES];
    
}

- (IBAction)txaction:(id)sender {

        [UIAlertView showWithTitle:nil message:@"请填写意见" style:UIAlertViewStylePlainTextInput cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            UITextField * textField = [alertView textFieldAtIndex:0];
            [textField resignFirstResponder];
            NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
            NSArray * array =@[@{@"unid":self.identifier},@{@"yj":textField.text},@{@"userId":userId}];
            if ([textField.text length] == 0) {
                return;
            }
            [HttpTools postWithURL:@"exchImg" arrays:array method:@"setSignYjRead" success:^(id JSON) {
            GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
            
            if ([message isEqualToString:@"20"]) {
                    [self.view.window makeToast:@"提交意见成功"];
//[self performSelector: @selector(popview) withObject: nil afterDelay:1.0f];
}else{
                [self.view.window makeToast:@"提交意见失败"];

}
            } failure:^(NSError *error) {
            }];
        }];

}

- (IBAction)qianshou:(id)sender {
                    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

_qianshoubutton.enabled = NO;
NSLog(@"111111111111111111");
  NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
                NSArray * array = @[@{@"userId":userId},@{@"unid":_identifier}];
                __weak typeof(self) weakSelf = self;
                [HttpTools requestWithURL:KexchexchangeURL arrays:array method:KsignExhcangeMethod success:^(id JSON) {
                    [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                      if ([[NSString stringWithFormat:@"%@",JSON[@"msg"]] isEqualToString:@"10"]) {
                   [weakSelf.view.window makeToast:@"签收成功"];
[_qianshou removeFromSuperview];
                         self.vcType = AlreadySignType;
                    [self requestData]; //刷新数据
                    
                      }else{
                                          [weakSelf.view.window makeToast:@"签收失败"];

                      }
                
                    
                } failure:^(NSError *error) {
                    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                    [weakSelf.view.window makeToast:@"签收失败"];
                }];
            }





- (IBAction)chuanyue:(id)sender {
          UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// if (_textString.length>0) {
//}else{
//    _textString = @"";
//
//}
    OANameListController * expendVC = (OANameListController*)[storyBoard instantiateViewControllerWithIdentifier:@"NameLIstID"];

//OAExpendViewController * expendVC = [[OAExpendViewController alloc] init];
//    expendVC.identifierString = listModel.path;
                         expendVC.hidesBottomBarWhenPushed = YES;
//                                 expendVC.type = UnitDataType;
expendVC.seltype = @"chuanyue";
//expendVC.textString = _textString;
expendVC.fileId = self.identifier;
    [self.navigationController pushViewController:expendVC animated:YES];

}
@end









